package com.xsedge.xsedge.service.impl;/*
 * @Author ljc
 * @Description //TODO $
 * @Param $
 * @return $
 **/

import com.xsedge.xsedge.dao.NodeRepository;
import com.xsedge.xsedge.entity.NodeInfo;
import com.xsedge.xsedge.service.KillDockerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

import static com.xsedge.xsedge.tools.SSHTools.sshConnection;

@Service
public class KillDockerServiceImp implements KillDockerService {

	@Autowired
	private NodeRepository nodeRepository;

	//kill一个服务，成功返回success ，失败返回error
	@Override
	public String killDocker(String containerId, String node_ip) throws IOException {
		NodeInfo nodeInfo = nodeRepository.findNodeByIp(node_ip);

		//查找内网穿透的服务器ip和内网穿透的映射port
		String ssh_mapping_ip = nodeInfo.getipMapping();
		int ssh_mapping_port = nodeInfo.getportMapping();

		String cmds = "docker kill "+containerId;
		//ssh 免密登录执行
		String ssh_res = sshConnection(ssh_mapping_ip, ssh_mapping_port, "xsedge", false, cmds);

		String result=null;
		if (ssh_res.contains("Error")){
			result ="error";
		}else {
			result = "success";
		}
		return result;
	}
}